/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.calculator;

import java.util.Iterator;

import com.reportgear.report.model.ReportModel;
import com.reportgear.report.model.cell.Cell;
import com.reportgear.report.model.cell.value.CrossDocDataObject;
import com.reportgear.report.model.cell.value.MainDetailObject;
import com.reportgear.report.model.cell.value.salary.SalaryDocObject;
import com.sot.core.util.StringUtils;

/**
 * 
 * 生成交叉薪酬档案格对象,不计算值
 * 
 * @version 1.0 2011-4-10
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class CrossDocDataObjectCalculator extends AbstractCalculator {
	public CrossDocDataObjectCalculator() {

	}

	@Override
	public ReportModel calculate(ReportModel model, CalcParameter parameter) {
		if (logger.isDebugEnabled()) {
			logger.debug("计算模型：电信薪酬档案对象计算");
		}

		ReportModel calcModel = model;

		this.doCalculate(calcModel);

		return calcModel;
	}

	/**
	 * 计算模型
	 * 
	 * @param model
	 *            模型
	 * @param mainObjList
	 *            Id列表
	 */
	private void doCalculate(ReportModel model) {
		for (Iterator<Cell> i = model.getCellIterator(); i.hasNext();) {
			Cell cell = i.next();

			if (cell.getColumn() > 0 && cell.getRow() > 0) {
				this.calculateCell(model, cell);
			}
		}

		this.calculateDocCell(model);
	}

	/**
	 * 所有SalaryDocObject对象Cell转换为CrosDocsDataObject对象
	 * 
	 * @param model
	 *            模型
	 * @param mainObjList
	 *            主对象Id列表
	 */
	private void calculateDocCell(ReportModel model) {
		for (Iterator<Cell> iter = model.getCellsIteratorByCellElementClass(SalaryDocObject.class); iter.hasNext();) {
			Cell cell = iter.next();

			int row = cell.getRow();
			int col = cell.getColumn();

			String field = ((SalaryDocObject) cell.getValue()).getField();
			int y = 0;
			for (int i = 0; i < col; i++) {
				Object v = model.getCell(i, row).getValue();

				if (v == null) {
					continue;
				}

				if (v instanceof MainDetailObject) {
					y = ((MainDetailObject) v).getId();

					break;
				}
			}

			if (y == 0) {
				return;
			}

			CrossDocDataObject docObj = new CrossDocDataObject(field, y);

			cell.setValue(docObj);
		}
	}

	/**
	 * 单元格计算
	 * 
	 * @param model
	 *            模型
	 * @param cell
	 *            单元格
	 * @param mainObjList
	 *            Id列表
	 */
	private void calculateCell(ReportModel model, Cell cell) {
		// 只计算空单元格
		if (!StringUtils.isEmpty(cell.getValue())) {
			return;
		}

		// 查找上面为SalaryDocObject，左边为MainDetailObject
		int row = cell.getRow();
		int col = cell.getColumn();

		String field = "";
		for (int i = 0; i < row; i++) {
			Object v = model.getCell(col, i).getValue();
			if (v == null) {
				continue;
			}

			if (v instanceof SalaryDocObject) {
				field = ((SalaryDocObject) v).getField();
				break;
			}
		}

		if (StringUtils.isEmpty(field)) {
			return;
		}

		int y = 0;
		for (int i = 0; i < col; i++) {
			Object v = model.getCell(i, row).getValue();

			if (v == null) {
				continue;
			}

			if (v instanceof MainDetailObject) {
				y = ((MainDetailObject) v).getId();

				break;
			}
		}

		if (y == 0) {
			return;
		}

		CrossDocDataObject docObj = new CrossDocDataObject(field, y);

		cell.setValue(docObj);
	}
}
